//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.controller.api;

import com.google.common.collect.Maps;
import com.xw.common.Const;
import com.xw.service.OrdersService;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/api/pay/")
@Scope("prototype")
public class ApiPayController {

    @Resource
    private OrdersService ordersService;

    /**
     * 余额支付
     * @param request
     * @param userId
     * @param orderNo   订单号
     * @param userPwd   登录密码
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "toBalancePay", method = RequestMethod.POST)
    @ResponseBody
    public Object toBalancePay(HttpServletRequest request, String userId, String orderNo,String userPwd) throws Exception {
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("orderNo", orderNo);
        map.put("userPwd", userPwd);
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = ordersService.toBalancePay(userId,orderNo, userPwd);
        return map;
    }


    /**
     * 付款接口
     * @param request
     * @param userId
     * @param orderNo   订单号
     * @param payType    1.支付宝 2.微信 3.小程序支付
     * @param openId    小程序支付 必传参数
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "toPay", method = RequestMethod.POST)
    @ResponseBody
    public Object toPay(HttpServletRequest request, String userId, String orderNo, Integer payType,String openId) throws Exception {
        //判断传参
        Map map = Maps.newHashMap();
        map.put("userId", userId);
        map.put("orderNo", orderNo);
        map.put("payType", payType);
        if (payType == 3){
            map.put("openId", openId);
        }
        String str = AppUtil.checkIsNull(map);
        if (!str.equals(Const.SUCCESS_CODE_STR)) {
            return JsonData.fail(Const.parameterError(str));
        }
        map.clear();
        map = ordersService.toPay(request,userId,orderNo,payType,openId);

        return map;
    }
}
